package com.hetwen.search.activity.base;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.ActionBarActivity;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.hetwen.search.R;
import com.hetwen.search.global.Constant;
import com.hetwen.search.log.Logger;
import com.umeng.analytics.MobclickAgent;

@SuppressWarnings("deprecation")
public abstract class BaseActivity extends ActionBarActivity {

	protected Context mContext;
	protected Handler mHandler;
	protected Activity mActivity;
	protected String mTag;
	protected SharedPreferences sp;
	protected Logger logger;
	
	private Dialog loadingDialog;
	private Animation loadingAnim;
	private ImageView loadingImage;
	private TextView loadingText;
	
	protected abstract void loadView();
	protected abstract void initView();
	protected abstract void initData();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContext = this;
		mHandler = new Handler();
		mActivity = this;
		mTag = getClass().getSimpleName();
		sp = getSharedPreferences(Constant.CONFIG_NAME, Context.MODE_PRIVATE);
		logger = Logger.getInstance();
		initLoadingDialog();
		loadView();
		initView();
		initData();
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		MobclickAgent.onPageStart(this.getClass().getSimpleName());
		MobclickAgent.onResume(this);
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		MobclickAgent.onPageEnd(this.getClass().getSimpleName());
		MobclickAgent.onPause(this);
	}
	
	private void initLoadingDialog(){
		loadingDialog = new Dialog(mContext,R.style.MyDialog);
		loadingDialog.setContentView(R.layout.dialog_loading);
		
		loadingImage = (ImageView) loadingDialog.findViewById(R.id.dialog_loading_image);
		loadingText = (TextView) loadingDialog.findViewById(R.id.dialog_loading_text);
		
		loadingAnim = AnimationUtils.loadAnimation(mContext, R.anim.loading);
		
		loadingDialog.setCancelable(false);
		loadingDialog.setCanceledOnTouchOutside(false);
	}
	
	protected void showLoadingDialog() {
		if(loadingDialog!=null && !loadingDialog.isShowing()){
			loadingDialog.show();
			loadingImage.startAnimation(loadingAnim);
		}
	}
	
	protected void showLoadingDialog(String text) {
		loadingText.setText(text);
		showLoadingDialog();
	}
	
	protected void closeLoadingDialog(){
		if(loadingDialog!=null && loadingDialog.isShowing()){
			loadingDialog.dismiss();
		}
	}
}
